package com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.entity.ErpFinanceexpenditure;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.entity.ErpFinanceexpenditurebill;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.mapper.ErpFinanceexpenditurebillMapper;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.dto.ErpFinanceexpenditureListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.mapper.ErpFinanceexpenditureMapper;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceExpenditure.service.IErpFinanceexpenditureService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-09-23
 */
@Service
@AllArgsConstructor
public class ErpFinanceexpenditureServiceImpl extends BaseService<ErpFinanceexpenditureMapper, ErpFinanceexpenditure> implements IErpFinanceexpenditureService {

	private ErpFinanceexpenditurebillMapper erpFinanceexpenditurebillMapper;

	@Override
	public IPage<ErpFinanceexpenditure> getPageList(ErpFinanceexpenditureListDto pageListDto) {
		Wrapper<ErpFinanceexpenditure> wrapper = Wrappers.<ErpFinanceexpenditure>query().lambda()				.eq(!StringUtil.isEmpty(pageListDto.getF_ExpenditureCode()), ErpFinanceexpenditure::getFExpenditurecode, pageListDto.getF_ExpenditureCode())
				.like(!StringUtil.isEmpty(pageListDto.getF_Manager()), ErpFinanceexpenditure::getFManager, pageListDto.getF_Manager())
				.eq(!StringUtil.isEmpty(pageListDto.getF_BusinessType()), ErpFinanceexpenditure::getFBusinesstype, pageListDto.getF_BusinessType());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addErpFinanceexpenditure(ErpFinanceexpenditure erpFinanceexpenditure, List<ErpFinanceexpenditurebill> erpFinanceexpenditurebillList) {
		String erpFinanceexpenditureId = IdWorker.get32UUID();
		erpFinanceexpenditure.setFId(erpFinanceexpenditureId);
		erpFinanceexpenditurebillList.forEach(erpFinanceexpenditurebill -> erpFinanceexpenditurebill.setFExpenditureid(erpFinanceexpenditureId));
		this.saveBatch(erpFinanceexpenditurebillList, ErpFinanceexpenditurebill.class, ErpFinanceexpenditurebillMapper.class);
		boolean isSuccess = this.save(erpFinanceexpenditure);
		return isSuccess ? erpFinanceexpenditureId : null;
	}

	@Override
	public boolean updateErpFinanceexpenditure(String id, ErpFinanceexpenditure erpFinanceexpenditure, List<ErpFinanceexpenditurebill> erpFinanceexpenditurebillList) {
		erpFinanceexpenditure.setFId(id);
		erpFinanceexpenditurebillList.forEach(erpFinanceexpenditurebill -> erpFinanceexpenditurebill.setFExpenditureid(id));
		erpFinanceexpenditurebillMapper.delete(Wrappers.<ErpFinanceexpenditurebill>query().lambda().eq(ErpFinanceexpenditurebill::getFExpenditureid, id));
		this.saveBatch(erpFinanceexpenditurebillList, ErpFinanceexpenditurebill.class, ErpFinanceexpenditurebillMapper.class);
		return this.updateById(erpFinanceexpenditure);
	}

	public List<ErpFinanceexpenditurebill> getErpFinanceexpenditurebillByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpFinanceexpenditurebill>query().lambda().eq(ErpFinanceexpenditurebill::getFExpenditureid, parentId);
		return erpFinanceexpenditurebillMapper.selectList(wrapper);
	}
}
